<template>
  <el-dialog
    :title="!dataForm.sjId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家账号" prop="sjName">
      <el-input v-model="dataForm.sjName" placeholder="商家账号"></el-input>
    </el-form-item>
    <el-form-item label="商家密码" prop="sjPwd">
      <el-input v-model="dataForm.sjPwd" placeholder="商家密码"></el-input>
    </el-form-item>
    <el-form-item label="商家房源" prop="sjHouse">
      <el-input v-model="dataForm.sjHouse" placeholder="商家房源"></el-input>
    </el-form-item>
    <el-form-item label="商家电话" prop="sjTel">
      <el-input v-model="dataForm.sjTel" placeholder="商家电话"></el-input>
    </el-form-item>
    <el-form-item label="新房Id" prop="newHouseid">
      <el-input v-model="dataForm.newHouseid" placeholder="新房Id"></el-input>
    </el-form-item>
    <el-form-item label="二手房Id" prop="oldHouseid">
      <el-input v-model="dataForm.oldHouseid" placeholder="二手房Id"></el-input>
    </el-form-item>
    <el-form-item label="租房Id" prop="rentHouseid">
      <el-input v-model="dataForm.rentHouseid" placeholder="租房Id"></el-input>
    </el-form-item>
    <el-form-item label="地区Id" prop="mapid">
      <el-input v-model="dataForm.mapid" placeholder="地区Id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="yhId">
      <el-input v-model="dataForm.yhId" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sjId: 0,
          sjName: '',
          sjPwd: '',
          sjHouse: '',
          sjTel: '',
          newHouseid: '',
          oldHouseid: '',
          rentHouseid: '',
          mapid: '',
          yhId: ''
        },
        dataRule: {
          sjName: [
            { required: true, message: '商家账号不能为空', trigger: 'blur' }
          ],
          sjPwd: [
            { required: true, message: '商家密码不能为空', trigger: 'blur' }
          ],
          sjHouse: [
            { required: true, message: '商家房源不能为空', trigger: 'blur' }
          ],
          sjTel: [
            { required: true, message: '商家电话不能为空', trigger: 'blur' }
          ],
          newHouseid: [
            { required: true, message: '新房Id不能为空', trigger: 'blur' }
          ],
          oldHouseid: [
            { required: true, message: '二手房Id不能为空', trigger: 'blur' }
          ],
          rentHouseid: [
            { required: true, message: '租房Id不能为空', trigger: 'blur' }
          ],
          mapid: [
            { required: true, message: '地区Id不能为空', trigger: 'blur' }
          ],
          yhId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sjId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sjId) {
            this.$http({
              url: this.$http.adornUrl(`//shoper/info/${this.dataForm.sjId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sjName = data.shoper.sjName
                this.dataForm.sjPwd = data.shoper.sjPwd
                this.dataForm.sjHouse = data.shoper.sjHouse
                this.dataForm.sjTel = data.shoper.sjTel
                this.dataForm.newHouseid = data.shoper.newHouseid
                this.dataForm.oldHouseid = data.shoper.oldHouseid
                this.dataForm.rentHouseid = data.shoper.rentHouseid
                this.dataForm.mapid = data.shoper.mapid
                this.dataForm.yhId = data.shoper.yhId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`//shoper/${!this.dataForm.sjId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sjId': this.dataForm.sjId || undefined,
                'sjName': this.dataForm.sjName,
                'sjPwd': this.dataForm.sjPwd,
                'sjHouse': this.dataForm.sjHouse,
                'sjTel': this.dataForm.sjTel,
                'newHouseid': this.dataForm.newHouseid,
                'oldHouseid': this.dataForm.oldHouseid,
                'rentHouseid': this.dataForm.rentHouseid,
                'mapid': this.dataForm.mapid,
                'yhId': this.dataForm.yhId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
